<template>
  <div class="app-container">
    <el-form inline ref="queryForm" :model="queryParams">
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" placeholder="功能名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 查询 </el-button>
        <el-button type="primary" @click="resetQuery"> 重置 </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="pageList" fit stripe height="615px">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="left"
      ></el-table-column>
      <el-table-column
        label="功能名称"
        prop="name"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column label="功能状态" width="140" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 1" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="API地址"
        prop="path"
        show-overflow-tooltip
        width="300"
        align="left"
      >
      </el-table-column>
      <el-table-column
        label="更新时间"
        prop="updatedAt"
        width="200"
        align="left"
      >
        <template slot-scope="scope">
          {{ scope.row.updatedAt | parseTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['/v1/rbac/action/update']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/v1/rbac/role/delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog
      append-to-body
      center
      width="800px"
      :visible.sync="visible"
      :title="title"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="功能名称" prop="name">
          <el-input v-model="form.name" placeholder="填写功能名称" />
        </el-form-item>
        <el-form-item label="功能类型" prop="method">
          <el-select v-model="form.method" placeholder="选择功能类型">
            <el-option label="GET" value="GET"></el-option>
            <el-option label="POST" value="POST"></el-option>
            <el-option label="PUT" value="PUT"></el-option>
            <el-option label="DELETE" value="DELETE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="API地址" prop="path">
          <el-input
            v-model="form.path"
            placeholder="API地址"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  actionList,
  createAction,
  updateAction,
  deleteAction,
} from "@/api/action";

export default {
  name: "Actionset",
  data() {
    return {
      queryParams: {
        page: 1,
        size: 10,
        name: undefined,
      },
      loading: false,
      total: 0,
      pageList: [],
      form: {},
      rules: {
        name: { required: true, message: "请填写功能名称", trigger: "blur" },
        method: {
          required: true,
          message: "请选择功能类型",
          trigger: "change",
        },
        path: { required: true, message: "请填写API地址", trigger: "blur" },
      },
      title: "新增",
      btnLoading: false,
      visible: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    cancel() {
      this.form = {};
      this.resetForm("form");
      this.visible = false;
    },
    handleDelete(row) {
      this.$confirm("确定要删除此项目吗？", "提示", { type: "warning" }).then(
        () => {
          deleteAction({ actionId: row.actionId }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        }
      );
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;

          if (this.form.actionId) {
            updateAction(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          } else {
            // 新增
            createAction(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          }
        }
      });
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    handleUpdate(row) {
      this.form = { ...row };
      this.title = "编辑";
      this.visible = true;
    },
    handleAdd() {
      this.form = {};
      this.resetForm("form");
      this.title = "新增";
      this.visible = true;
    },
    getList() {
      this.loading = true;

      actionList(this.queryParams)
        .then((res) => {
          this.pageList = res.data.items || [];
          this.total = res.data.total || 0;
        })
        .finally(() => (this.loading = false));
    },
  },
};
</script>

<style></style>
